package linwen.wang.pojo;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.Date;

/**
 * @ClassName User
 * @Description User
 * @Author zero
 * @Date 2022/3/2 下午10:42
 * @Version V1.0
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class User {


    // groups：标识此校验规则属于哪个分组，可以指定多个分组
    @NotNull(groups = Update.class)
    @Min(value = 10000, groups = Update.class)
    private Integer id;

    // 每一个注解都包含了message字段，用于校验失败时作为提示信息。不写message将使用默认的错误提示信息。
    @NotNull(groups = {Save.class, Update.class})
    @Size(min = 5, max = 10, groups = {Save.class, Update.class}, message = "请输入5-10个字符的用户名")
    private String username;

    private String password;

    @Min(18)
    private Integer age;

    @NotBlank(message = "手机号码不能为空", groups = {Save.class})
    @Pattern(regexp = "^1(3|4|5|7|8)\\d{9}$", message = "手机号码格式错误")
    private String phone;

    @Email(message = "邮箱格式错误")
    private String email;

    @NotNull(message = "生日不能为空")
    @Past // 生日必须是一个过去的时间
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birthday;

    @Valid
    @NotNull(groups = {Save.class, Update.class})
    private Job job;

    @Data
    public static class Job {

        @NotNull(groups = {Update.class})
        @Min(value = 1, groups = Update.class)
        private Long jobId;

        @NotNull(groups = {Save.class, Update.class})
        @Length(min = 2, max = 10, groups = {Save.class, Update.class})
        private String jobName;

        @NotNull(groups = {Save.class, Update.class})
        @Length(min = 2, max = 10, groups = {Save.class, Update.class})
        private String position;
    }


    /**
     * 校验分组中不需要定义任何方法，该接口仅仅是为了区分不同的校验规则
     * 保存的时候校验分组
     */
    public interface Save {
    }

    /**
     * 校验分组中不需要定义任何方法，该接口仅仅是为了区分不同的校验规则
     * 更新的时候校验分组
     */
    public interface Update {
    }

}
